﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AlmKickstart.Data;
using AlmKickstart.Web.Infrastructure;

namespace AlmKickstart.Web.Controllers
{
	public class ProjectController : AlmKickstartController
	{
		//
		// GET: /Project/
		public ViewResult Index(int? projectType)
		{
			IQueryable<Project> projects;
			if (projectType.HasValue)
			{
				projects = db.Projects.Where(p => p.ProjectTypeId == projectType);
				var type = db.ProjectTypes.Single(s => s.ProjectTypeId == projectType);
				ViewBag.ProjectType = type.PluralName;
				ViewBag.ProjectTypeId = type.ProjectTypeId;
			}
			else
			{
				projects = db.Projects.Include(p => p.Status).Include(p => p.ProjectType);
				ViewBag.ProjectType = "All Items";
			}

			return View(projects.ToList());
		}

		//
		// GET: /Project/Create
		public ActionResult Create(int? projectType, int? parentProject)
		{
			ViewBag.StatusId = new SelectList(db.ProjectStatuses, "StatusId", "Name");
			ViewBag.ProjectTypeId = new SelectList(db.ProjectTypes, "ProjectTypeId", "Name");

			if(parentProject.HasValue)
			{
				var parent = db.Projects.Single(s => s.ProjectId == parentProject.Value);
				var type = db.ProjectTypes.Single(s => s.ProjectTypeId == projectType.Value);
				var model = new Project() { ProjectType = type, ProjectTypeId = projectType.Value, 
					ParentProjectId = parentProject.Value };
				return View(model);
			}
			else if(projectType.HasValue)
			{
				var type = db.ProjectTypes.Single(s => s.ProjectTypeId == projectType.Value);
				var model = new Project() { ProjectType = type, ProjectTypeId = projectType.Value };
				return View(model);
			}

			return View();
		}

		//
		// POST: /Project/Create
		[HttpPost]
		public ActionResult Create(Project project)
		{
			db.Projects.Add(project);
			db.SaveChanges();
			return RedirectToAction("Edit", new { id = project.ProjectId });
		}

		//
		// GET: /Project/Edit/5
		public ActionResult Edit(int id)
		{
			Project project = db.Projects.Include(i => i.ParentProject).Single(s => s.ProjectId == id);

			ViewBag.StatusId = new SelectList(db.ProjectStatuses, "StatusId", "Name", project.StatusId);
			ViewBag.ProjectTypeId = new SelectList(db.ProjectTypes, "ProjectTypeId", "Name", project.ProjectTypeId);
			return View(project);
		}

		//
		// POST: /Project/Edit/5
		[HttpPost]
		public ActionResult Edit(int id, FormCollection formValues)
		{
			if (ModelState.IsValid)
			{
				var model = db.Projects.Single(s => s.ProjectId == id);
				UpdateModel(model, formValues);
				db.SaveChanges();
				return RedirectToAction("Edit", new { id = id });
			}
			return View();
		}

		//
		// GET: /Project/List/5
		public ActionResult List(int id)
		{
			var parent = db.Projects.Single(s => s.ProjectId == id);
			if (db.ProjectTypes.Count(c => c.ParentProjectTypeId == parent.ProjectTypeId) == 0)
				return Content("");
			var projects = db.Projects.Where(p => p.ParentProjectId == id);
			ViewBag.ParentProjectId = id;
			var projectType = db.ProjectTypes.Single(s => s.ParentProjectTypeId == parent.ProjectTypeId);
			ViewBag.ProjectType = projectType.PluralName;
			ViewBag.ProjectTypeId = projectType.ProjectTypeId;
			return PartialView(projects.ToList());
		}
	}
}